package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.MessageCheckOut;

public class MessageCheckOutLogic extends AbstractBL{
	public List<MessageCheckOut> GenerateMessageFolioCheckout(long hotelId)
	{
		List<MessageCheckOut> list = new ArrayList<MessageCheckOut>();
		
		MessageCheckOut messagefolioCheckout;
		if(GetMessageCheckout(1,"Thank!") == null){
			messagefolioCheckout = new MessageCheckOut();
			messagefolioCheckout.setHotelId(hotelId);
			messagefolioCheckout.setMessage("Thank!");
			messagefolioCheckout.setThxmessage("Thank you very much.");
			list.add(messagefolioCheckout);
		}
		
		return list;
	}
	
	public MessageCheckOut GetMessageCheckout(long hotelId,String title)
	{
		Query q = new Query(EntityName.MessageCheckOut);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("message",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetMessage(datastore.prepare(q).asSingleEntity());
	}
	
	public MessageCheckOut GetMessage(Entity entity)
	{
		if(entity == null) return null;
		MessageCheckOut item = new MessageCheckOut();
		item.setEntity(entity);
		return item;
	}
	
	public MessageCheckOut AddMessageCheckOut(long hotelId,String txtMessage,String thxMessage) throws EntityNotFoundException{
		MessageCheckOut b = new MessageCheckOut();
		b.setHotelId(hotelId);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public MessageCheckOut EditMessageCheckOut(long id,String txtMessage,String thxMessage,String pic) throws EntityNotFoundException{
		MessageCheckOut b =  GetMessageCheckOut(id);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public MessageCheckOut GetMessageCheckOut(long id)
	{
		Key key = KeyFactory.createKey(EntityName.MessageCheckOut, id);
		try {
			Entity lang = datastore.get(key);
			MessageCheckOut c = new MessageCheckOut();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<MessageCheckOut> GetMessageCheckOutAll(long hotelId) {
		Query q = new Query(EntityName.MessageCheckOut);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		q.setFilter(filterHotel);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<MessageCheckOut> GetList(List<Entity> entities) {
		List<MessageCheckOut> list = new ArrayList<MessageCheckOut>();
		if(entities!=null)
		for (Entity e : entities) {
			MessageCheckOut a = new MessageCheckOut();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
